#include "qtrendpanelwidget.h"

#include "../../libs/core/qsoftcore.h"
#include "../../libs/machine/qmachine.h"
#include "../../libs/machine/qmachinevalue.h"

#include <QPainter>

#include <QHBoxLayout>
#include <QColorDialog>

QTrendPanelWidget::QTrendPanelWidget(QWidget *parent) :
    QWidget(parent),
    m_wave_widget(new QWaveWidget(this)),
    m_time_count(125)
{
    QHBoxLayout *v=new QHBoxLayout;
    v->setContentsMargins(100,30,100,50);
    v->setSpacing(0);
    v->addWidget(m_wave_widget);
    setLayout(v);
    connect(m_wave_widget,SIGNAL(need_update()),this,SLOT(update()));
    connect(m_wave_widget,SIGNAL(clicked_value(QString,QString,QString,QString)),
            this,SIGNAL(clicked_value(QString,QString,QString,QString)));

    for(int i=0;i<4;i++)
    {
        m_config[i].m_visible=true;
    }
    connect(m_wave_widget,SIGNAL(status_changed(int)),this,SIGNAL(status_changed(int)));
}

void QTrendPanelWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

    p.fillRect(this->rect(),QColor(192,192,192));

    QRect r=m_wave_widget->geometry();
    r.adjust(-1,-1,1,1);
    p.setPen(QColor(0,0,0));
    p.setBrush(QColor(150,150,150));
    p.drawRect(r);

    int start=m_wave_widget->x_start();
    QString str;
    QList<int>   pts;

    QPointF ptf=m_wave_widget->m_data_item[0]->pos();

    int s=-ptf.x()*m_wave_widget->m_tatal_count/m_wave_widget->width();
    int t;

    for(int i=0;i<=10;i++)
    {
        int x;
        if(m_wave_widget->m_tatal_count>10)
        {
            x=(s+m_wave_widget->m_count_x*i/10)*m_wave_widget->width()*m_wave_widget->m_scale_x/m_wave_widget->m_tatal_count;
            x+=ptf.x();
        }
        else
        {
            x=m_wave_widget->width()*i/10;
        }
        pts.append(x);
        p.drawLine(x,r.bottom()+1,x,r.bottom()+11);
        t=s*m_time_count;
        str.sprintf("%d.%03d",t/1000,t%1000);
        p.drawText(QRect(x-r.width()/20,r.bottom()+15,r.width()/10,30),
                       Qt::AlignHCenter | Qt::AlignTop,str);
    }

    p.drawText(QRect(r.right()+5,r.bottom()+30,30,30),Qt::AlignLeft | Qt::AlignVCenter,"(ms)");


    double d_min,d_max;

    for(int i=0;i<=5;i++)
    {
        p.drawLine(r.left()-10,r.top()+r.height()*i/5,r.left()-1,r.top()+r.height()*i/5);
        p.drawLine(r.right()+10,r.top()+r.height()*i/5,r.right()+1,r.top()+r.height()*i/5);
    }

    if(m_config[0].m_visible)
    {
        d_min=m_wave_widget->get_min(0);
        d_max=m_wave_widget->get_max(0);

        double min,max;
        double d=(d_max-d_min)/m_wave_widget->m_scale_y;
        max=d_max+d*ptf.y()*m_wave_widget->m_scale_y/m_wave_widget->height();
        min=max-d;

        p.setPen(m_config[0].m_color);

        for(int i=0;i<=5;i++)
        {
            str.sprintf("%0.6f",min+d*i/5);
            p.drawText(QRect(r.left()-100,r.bottom()-r.height()*i/5-12,87,24),m_config[1].m_visible?(Qt::AlignRight | Qt::AlignTop)
                                                                                        :(Qt::AlignRight | Qt::AlignVCenter),
                       str);
        }
    }

    if(m_config[1].m_visible)
    {
        d_min=m_wave_widget->get_min(1);
        d_max=m_wave_widget->get_max(1);


        double min,max;
        double d=(d_max-d_min)/m_wave_widget->m_scale_y;
        max=d_max+d*ptf.y()*m_wave_widget->m_scale_y/m_wave_widget->height();
        min=max-d;

        p.setPen(m_config[1].m_color);

        for(int i=0;i<=5;i++)
        {
            str.sprintf("%0.6f",min+(max-min)*i/5);
            p.drawText(QRect(r.left()-100,r.bottom()-r.height()*i/5-12,87,24),m_config[0].m_visible?(Qt::AlignRight | Qt::AlignBottom)
                                                                                        :(Qt::AlignRight | Qt::AlignVCenter),
                       str);
        }
    }


    if(m_config[2].m_visible)
    {
        d_min=m_wave_widget->get_min(2);
        d_max=m_wave_widget->get_max(2);

        double min,max;
        double d=(d_max-d_min)/m_wave_widget->m_scale_y;
        max=d_max+d*ptf.y()*m_wave_widget->m_scale_y/m_wave_widget->height();
        min=max-d;

        p.setPen(m_config[2].m_color);

        for(int i=0;i<=5;i++)
        {
            str.sprintf("%0.6f",min+(max-min)*i/5);
            p.drawText(QRect(r.right()+13,r.bottom()-r.height()*i/5-12,87,24),m_config[3].m_visible?(Qt::AlignLeft | Qt::AlignTop)
                                                                                        :(Qt::AlignLeft | Qt::AlignVCenter),
                       str);
        }
    }

    if(m_config[3].m_visible)
    {
        d_min=m_wave_widget->get_min(3);
        d_max=m_wave_widget->get_max(3);

        double min,max;
        double d=(d_max-d_min)/m_wave_widget->m_scale_y;
        max=d_max+d*ptf.y()*m_wave_widget->m_scale_y/m_wave_widget->height();
        min=max-d;

        p.setPen(m_config[3].m_color);

        for(int i=0;i<=5;i++)
        {
            str.sprintf("%0.6f",min+(max-min)*i/5);
            p.drawText(QRect(r.right()+13,r.bottom()-r.height()*i/5-12,87,24),m_config[2].m_visible?(Qt::AlignLeft | Qt::AlignBottom)
                                                                                        :(Qt::AlignLeft | Qt::AlignVCenter),
                       str);
        }
    }


    pts.removeFirst();
    pts.removeLast();

    QPen pen(QColor(192,192,192),1,Qt::DotLine);
    p.setPen(pen);
    for(int i=0;i<pts.size();i++)
    {
        p.drawLine(pts.at(i),r.top()+1,pts.at(i),r.bottom());
    }

    for(int i=1;i<5;i++)
    {
        p.drawLine(r.left()+1,r.top()+r.height()*i/5,r.right(),r.top()+r.height()*i/5);
    }
}

void QTrendPanelWidget::set_color(int line)
{
    QColor c=QColorDialog::getColor(m_config[line].m_color,this);
    if(c.isValid())
    {
        m_config[line].m_color=c;
        m_wave_widget->set_color(line,c);

        int co=(c.red()<<16)+(c.green()<<8)+c.blue();

        QSoftCore::get_core()->set_setting_value(QString("Trend.Line_%1_color").arg(line+1),co);

        update();
    }
}

void QTrendPanelWidget::set_visible(int line, bool visible)
{
    m_config[line].m_visible=visible;
    m_wave_widget->set_visible(line,visible);
    update();
}

void QTrendPanelWidget::set_LButton_type(int type)
{
    m_wave_widget->set_LButton_type(type);
}

void QTrendPanelWidget::scale(double l, double t,double r,double b)
{
    m_wave_widget->scale(l,t,r,b);
}

void QTrendPanelWidget::reset()
{
    m_wave_widget->reset();
}

void QTrendPanelWidget::set_color()
{
    for(int i=0;i<4;i++)
    {
        m_wave_widget->set_color(i,m_config[i].m_color);
    }
}

void QTrendPanelWidget::clear()
{
    m_wave_widget->clear();
}

enTrendState QTrendPanelWidget::get_status()
{
    return m_wave_widget->get_status();
}

void QTrendPanelWidget::set_status(enTrendState status)
{
    m_wave_widget->set_status(status);
}

void QTrendPanelWidget::set_value(double *value, int line_count, int data_count)
{
    m_wave_widget->set_value(value,line_count,data_count);
}
